import { fileURLToPath, URL } from 'node:url'
// path 模块提供了一些工具函数，用于处理文件与目录的路径
import { resolve } from 'path'
import { defineConfig } from 'vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vue from '@vitejs/plugin-vue'

/** 当前执行 node 命令时文件夹的地址（工作目录） */
const root: string = process.cwd()

/** 路径拼接函数，简化代码 */
const pathResolve = (dir: string): string => resolve(root, dir)

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    })
  ],
  resolve: {
    alias: [
      // 设置 `@` 指向 `src` 目录
      { find: '@', replacement: pathResolve('src') },
      // 设置 `@assets` 指向 `src/assets` 目录
      { find: '@assets', replacement: pathResolve('src/assets') },
      // 设置 `@components` 指向 `src/components` 目录
      { find: '@components', replacement: pathResolve('src/components') },
      // 设置 `@views` 指向 `src/views` 目录
      { find: '@views', replacement: pathResolve('src/views') },
      // 设置 `@utils` 指向 `src/utils` 目录
      { find: '@utils', replacement: pathResolve('src/utils') }
      // 设置 `@store` 指向 `src/store` 目录
      //{ find: '@store', replacement: pathResolve('src/store') }
      // 可以根据需要添加更多的别名
    ]
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: '@import "@/assets/style/main.scss";'
      }
    }
  }
})
